package com.learn.jvm.gc;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.List;

/**
 * testGC() 方法执行后，objA和objB会不会被GC呢？
 *
 * -XX:+PrintGCDetails -Xms100m -Xmx100m
 */
public class ReferenceCountingGC {

    public Object instance = null;
    private static final int _1MB = 1024 * 1024;
    // 这个成员属性的唯一意义就是占用内存，以便能在GC中看清楚是否有回收过
    private byte[] bigSize = new byte[2 * _1MB];

    public static void main(String[] args) {
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        List<String> inputArguments = runtimeMXBean.getInputArguments();
        inputArguments.stream().forEach(System.out::println);
        ReferenceCountingGC objA = new ReferenceCountingGC();
        ReferenceCountingGC objB = new ReferenceCountingGC();
        objA.instance = objB;
        objB.instance = objA;

        objA = null;
        objB = null;
        // 假设这行发生了GC，objA和objB是否被回收？
        System.gc();
    }

}
